package com.ys.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.RectangleReadOnly;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import com.ys.config.MyConfig;
import com.ys.entity.base.TbFileInfo;
import lombok.AllArgsConstructor;
import lombok.Data;

public class PdfUtil {
	
	public static void main(String[] args) throws Exception {
		
//		generatePeiHuo(dir, pdfFileName);
//		generateZuzhuang(dir, pdfFileName, null);
		generate1Pdf("D:/data/upload/pdf/", "test.pdf");
	}
	
	/**
	 * 
	 * @param dir 目录
	 * @param pdfFileName PDF的文件名
	 */
	public static void generateFinancePdf(String dir, String pdfFileName, BaoXiaoDto dto) {
		
		Rectangle rec = new RectangleReadOnly(420, 600);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			
			//4.生成PDF
			document.newPage();
//			document.setPageCount(i);//设置生成pdf页数
			new PdfUtil().generateFinance(document, dto);
			
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param dir 目录
	 * @param pdfFileName PDF的文件名
	 */
	public static void generate1Pdf(String dir, String pdfFileName) {
		
		Rectangle rec = new RectangleReadOnly(420, 600);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			
			//4.生成PDF
			document.newPage();
//			document.setPageCount(i);//设置生成pdf页数
			new PdfUtil().generate1(document);
			
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 定义全局的字体静态变量
	private static BaseFont bfChinese = createFontLocal("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

	private static Font textfont4 = new Font(bfChinese, 4, Font.NORMAL);
	private static Font textfont6 = new Font(bfChinese, 6, Font.NORMAL);
	private static Font textfont8 = new Font(bfChinese, 8, Font.NORMAL);
	private static Font textfont8W = new Font(bfChinese, 8, Font.BOLD);
	private static Font textfont10 = new Font(bfChinese, 10, Font.NORMAL);
//	private static Font textfont11 = new Font(bfChinese, 11, Font.NORMAL);
	private static Font textfont12 = new Font(bfChinese, 12, Font.NORMAL);
	private static Font textfont14 = new Font(bfChinese, 14, Font.NORMAL);
	private static Font textfont14W = new Font(bfChinese, 14, Font.BOLD);
	private static Font textfont16 = new Font(bfChinese, 16, Font.NORMAL);
	private static Font textfont16Bold = new Font(bfChinese, 16, Font.BOLD);
	private static Font textfont18 = new Font(bfChinese, 18, Font.NORMAL);
	
	private static Font headfont;
	private static Font keyfont;
	private static Font textfont;

	// 静态代码块
	static {
		try {
			// 不同字体（这里定义为同一种字体：包含不同字号、不同style）
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			headfont = new Font(bfChinese, 20, Font.NORMAL);
			keyfont = new Font(bfChinese, 10, Font.BOLD);
			textfont = new Font(bfChinese, 15, Font.NORMAL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 本地创建字体对象
	 */
	public static BaseFont createFontLocal(String name, String encoding, boolean embedded) {
		try {
			return BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 *  生成PDF：职能费用申请单/合作商付款/运营费用申请
	 */
	public void generate1(Document document) throws Exception {
	
		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{100, 100, 100, 100, 100, 100}, 1, 400);
			
			table1.addCell(createCell("问前集团控股有限公司", textfont14W, Element.ALIGN_CENTER, 6, 1));
			table1.addCell(createCell("WEN QIAN GROUP HOLDINGS LIMITED", textfont14, Element.ALIGN_CENTER, 6, 1));
			table1.addCell(createCell("服务发票", textfont14W, Element.ALIGN_CENTER, 6, 1));
			table1.addCell(createCell("Sales Invoice", textfont14, Element.ALIGN_CENTER, 6, 1));
			
			table1.addCell(createCell("TO:", textfont8W, Element.ALIGN_CENTER, 1, 2, 1));
			table1.addCell(createCell("XXX公司", textfont8, Element.ALIGN_CENTER, 2, 2, 1));
			table1.addCell(createCell("发票号码", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("GS8888", textfont8, Element.ALIGN_CENTER, 2, 2, 1));
			table1.addCell(createCell("Invoice No.", textfont8W, Element.ALIGN_CENTER, 1, 1));
			
			table1.addCell(createCell("总金额(CNY)", textfont8W, Element.ALIGN_CENTER, 1, 1, 1));
			table1.addCell(createCell("90", textfont8, Element.ALIGN_CENTER, 2, 2, 1));
			table1.addCell(createCell("发票日期", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("2024/7/5", textfont8, Element.ALIGN_CENTER, 2, 2, 1));
			table1.addCell(createCell("Total Amount", textfont8W, Element.ALIGN_CENTER, 1, 1, 1));
			table1.addCell(createCell("Date of Invoice", textfont8W, Element.ALIGN_CENTER, 1, 1));
			
			table1.addCell(createCell("原产地", textfont8W, Element.ALIGN_CENTER, 1, 1, 1));
			table1.addCell(createCell("CHINA", textfont8, Element.ALIGN_CENTER, 2, 2, 1));
			table1.addCell(createCell("公司编码", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("GS8888", textfont8, Element.ALIGN_CENTER, 2, 2, 1));
			table1.addCell(createCell("country Of Origin", textfont8W, Element.ALIGN_CENTER, 1, 1, 1));
			table1.addCell(createCell("Company Code", textfont8W, Element.ALIGN_CENTER, 1, 1));

			table1.addCell(createCell("商品编码", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("品名描述", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("数量", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("单位", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("单价(CNY)", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("金额(CNY)", textfont8W, Element.ALIGN_CENTER, 1, 1));

			table1.addCell(createCell("Product Code", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("Description", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("Qity", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("Company", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("Unit Price", textfont8W, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("Amount", textfont8W, Element.ALIGN_CENTER, 1, 1));

			table1.addCell(createCell("G1234567", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("皮带 5条装", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("1", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("件", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("30", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("30", textfont8, Element.ALIGN_CENTER, 1, 1));

			table1.addCell(createCell("G1234567", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("皮带 5条装", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("1", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("件", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("30", textfont8, Element.ALIGN_CENTER, 1, 1));
			table1.addCell(createCell("30", textfont8, Element.ALIGN_CENTER, 1, 1));
			
		}
		document.add(table1);
	}
	
	/**
	 *  生成PDF：职能费用申请单/合作商付款/运营费用申请
	 */
	public void generateFinance(Document document, BaoXiaoDto dto) throws Exception {
	
		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{80, 60, 100, 60, 50}, 1, 400);
			
			table1.addCell(createCell(dto.getTitle(), textfont14, Element.ALIGN_CENTER, 5, 0));
			table1.addCell(createCell(dto.getCreateCompany(), textfont8, Element.ALIGN_LEFT, 3, 0));
			table1.addCell(createCell("申请时间：" + dto.getCreateTime(), textfont8, Element.ALIGN_LEFT, 2, 0));

			table1.addCell(createCell("单据编号：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getCode(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("申请人：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getApplyUser(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("申请部门：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getApplyDept(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("收款账号：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayAccount(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款类型：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayType(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款金额：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayAmount(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款说明：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getRemark(), textfont8, Element.ALIGN_CENTER, 4, 1));
			
			List<BaoXiaoDto.DetailDto> detailArr = dto.getDetailArr();
			if (detailArr != null && detailArr.size() > 0) {

				table1.addCell(createCell("报销明细", textfont8, Element.ALIGN_LEFT, 5, 0));
				
				table1.addCell(createCell("工号及姓名", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("运营公司", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("用途", textfont8, Element.ALIGN_CENTER, 2, 1));
				table1.addCell(createCell("报销金额", textfont8, Element.ALIGN_CENTER, 1));
				
				for(BaoXiaoDto.DetailDto detail : detailArr) {

					table1.addCell(createCell(detail.getUser(), textfont8, Element.ALIGN_CENTER, 1));
					table1.addCell(createCell(detail.getOperatorCompanay(), textfont8, Element.ALIGN_CENTER, 1));
					table1.addCell(createCell(detail.getRemark(), textfont8, Element.ALIGN_CENTER, 2, 1));
					table1.addCell(createCell(detail.getAmount(), textfont8, Element.ALIGN_CENTER, 1));
				}
				
			}

			table1.addCell(createCell("审核明细", textfont8, Element.ALIGN_LEFT, 5, 0));
			{

				table1.addCell(createCell("处理时间", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理人", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理结果", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理意见", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("附件", textfont8, Element.ALIGN_CENTER, 1));
			}
			
			for(BaoXiaoDto.RowDto row : dto.getRows()) {

				table1.addCell(createCell(row.getOpeTime(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeUser(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeResult(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeOpinion(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getFileName(), textfont8, Element.ALIGN_CENTER, 1));
			}
			
			{
				table1.addCell(createCell("打印时间：" + dto.getPrintTime(), textfont8, Element.ALIGN_LEFT, 2, 0));
				table1.addCell(createCell("打印人：" + dto.getPrintUser() + "   " + "打印人部门：" + dto.getPrintDept(), textfont8, Element.ALIGN_LEFT, 3, 0));
			}
		}
		document.add(table1);
	}
	@Data
	@AllArgsConstructor
	public static class BaoXiaoDto{
		private String title; // 单据标题
		private String createCompany; // 制单公司
		private String createTime; // 申请时间
		private String code; // 单据编号
		private String applyUser; // 申请人
		private String applyDept; // 申请部门
		private String payType; // 支付类型
		private String payAccount; // 支付账号
		private String payAmount; // 支付金额
		private String remark; // 付款说明
		private String printTime; // 打印时间
		private String printUser; // 打印人
		private String printDept; // 打印部门
		private List<DetailDto> detailArr; // 报销明细数组
		private List<RowDto> rows; // 处理明细
		
		@Data
		@AllArgsConstructor
		public static class DetailDto {
			private String user; // 账户所属
			private String operatorCompanay; // 运营公司
			private String remark; // 用途
			private String amount; // 报销金额
		}
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private String opeTime; // 处理时间
			private String opeUser; // 商品名称
			private String opeResult; // FNSKU
			private String opeOpinion; // 计划发货数量
			private String fileName; // 附件名
		}
	}
	
	/**
	 * 创建指定列宽、列数的表格
	 */
	public static PdfPTable createTable(float[] widths, Integer border, Integer totalWidth) {
	    PdfPTable table = new PdfPTable(widths);
	    try {
	        table.setTotalWidth(totalWidth);
	        table.setLockedWidth(true);
	        table.setHorizontalAlignment(Element.ALIGN_CENTER);
	        table.getDefaultCell().setBorder(border);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return table;
	}

	/**
	 * 创建指定的图片格式
	 */
	public static PdfPCell createPdfCell(String path) {
		PdfPCell acell = null;
		try {
			Image image1 = Image.getInstance(path);
			image1.setAlignment(Image.ALIGN_CENTER);
			// 缩放图像到30%的大小
			image1.scalePercent(5);
			acell = new PdfPCell(image1,false);
			acell.setVerticalAlignment(Element.ALIGN_MIDDLE);
			acell.setHorizontalAlignment(Element.ALIGN_CENTER);
			acell.setBorder(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return acell;
	}

	/**
	 * 创建空白的行
	 */
	public PdfPCell createBlankCell() {
		PdfPCell acell = new PdfPCell();
		acell.setBorder(0);
		return acell;
	}

	/**
	 * 创建空白的表格
	 */
	public static PdfPTable createBlankTable(float spacing) {
	    PdfPTable table = new PdfPTable(1);
	    table.getDefaultCell().setBorder(0);
	    table.addCell(createCell("", keyfont, 0));
	    table.setSpacingAfter(spacing);
	    table.setSpacingBefore(spacing);
	    return table;
	}

	/**
	 * 创建单元格(指定字体)
	 */
	public static PdfPCell createCell(String value, Font font, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
//		cell.disableBorderSide(side); // 1隐上2隐下3隐上下4隐左8隐右
		return cell;
	}
	
	/**
	 * 创建单元格（指定字体、水平..）
	 */
	public static PdfPCell createCell(String value, Font font, int align, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平居..、单元格跨x列合并）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param colspan
	 * @return
	 */
	public static PdfPCell createCell(String value, Font font, int align, int colspan, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setColspan(colspan);
	    cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平居..、单元格跨x列合并）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param rowspan
	 * @return
	 */
	public static PdfPCell createCell1(String value, Font font, int align, Integer border,int rowspan) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(align);
		cell.setRowspan(rowspan);
		cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}

	/**
	 * 创建单元格（指定字体、水平居..、单元格跨x列合并）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param colspan
	 * @return
	 */
	public static PdfPCell createCell(String value, Font font, int align, int colspan, int rowspan, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setColspan(colspan);
	    cell.setRowspan(rowspan);
	    cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param colspan
	 * @param boderFlag
	 * @return
	 */
	public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setColspan(colspan);
	    cell.setPhrase(new Phrase(value, font));
	    cell.setPadding(3.0f);
	    if (!boderFlag) {
	        cell.setPaddingTop(15.0f);
	        cell.setPaddingBottom(8.0f);
	    } else if (boderFlag) {
	        cell.setPaddingTop(0.0f);
	        cell.setPaddingBottom(15.0f);
	    }
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平..、边框宽度：0表示无边框、内边距）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param borderWidth
	 * @param paddingSize
	 * @param flag
	 * @return
	 */
	public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setPhrase(new Phrase(value, font));
	    cell.setBorderWidthLeft(borderWidth[0]);
	    cell.setBorderWidthRight(borderWidth[1]);
	    cell.setBorderWidthTop(borderWidth[2]);
	    cell.setBorderWidthBottom(borderWidth[3]);
	    cell.setPaddingTop(paddingSize[0]);
	    cell.setPaddingBottom(paddingSize[1]);
	    if (flag) {
	        cell.setColspan(2);
	    }
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}
	
	/*
	 * 创建单元格(指定字体)
	 */
	public PdfPCell createCellImg(Image image, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setImage(image);
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}
	
	/*
	 * 创建单元格(图片，跨列)
	 */
	public PdfPCell createCellImg(Image image, Integer rowspan, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setImage(image);
		if (rowspan != null && rowspan > 1) {
			cell.setRowspan(rowspan);
		}
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}
	
	/*
	 * 创建单元格(条形码，跨列)
	 */
	public static PdfPCell createCellBarcode(PdfWriter pdfWriter, String barcodeText, Integer rowspan, Integer colspan, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setPadding(20f);

	    Barcode128 code128 = new Barcode128();
	    code128.setCode(barcodeText);
	    code128.setCodeType(Barcode128.CODE128);
	    code128.setBarHeight(30f);
	    Image code128Image = code128.createImageWithBarcode(pdfWriter.getDirectContent(), null, null);
	    code128Image.setAlignment(Image.ALIGN_CENTER);
	    code128Image.scalePercent(20);
		cell.setImage(code128Image);
		if (rowspan != null && rowspan > 1) {
			cell.setRowspan(rowspan);
		}
		if (colspan != null && colspan > 1) {
			cell.setColspan(colspan);
		}
		
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}

	/**
	 * 创建单元格(指定字体、水平对齐、合并行单元格)
	 */
	public PdfPCell createCellText(String value, Font font, Integer horizontalAlignment, Integer colspan, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(horizontalAlignment);
		cell.setPhrase(new Phrase(value, font));
		cell.setColspan(colspan);
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}

	/**
	 * 创建单元格（指定字体、水平..）
	 */
	public static PdfPCell createCellHeight(String value, Font font, int align, Integer border,float minimumHeight) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(align);
		cell.setMinimumHeight(minimumHeight);
		cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}
	
	public static PdfPCell handelBorder(PdfPCell cell, float borderWidth) {
        cell.setBorder(0);
        cell.setBorderWidthTop(borderWidth);
        cell.setBorderWidthBottom(borderWidth);
        cell.setBorderWidthLeft(borderWidth);
        cell.setBorderWidthRight(borderWidth);
        cell.setBorderColorBottom(BaseColor.BLACK);
        cell.setBorderColorLeft(BaseColor.BLACK);
        cell.setBorderColorRight(BaseColor.BLACK);
        cell.setBorderColorTop(BaseColor.BLACK);
		return cell;
	}


	/**
	 * FBA装箱单 - 配货清单-打印
	 */
	public static String generatePeiHuo(String fileName, PeihuoDto dto) {

		Rectangle rec = new RectangleReadOnly(420, 600);

		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			String dir = MyConfig.getProfile();
			String pdfFileName = "/pdf/" + fileName + ".pdf";
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();

			//4.生成PDF
			for (int i = 0; i < dto.itemArr.size(); i++) {

				document.newPage();
//				document.setPageCount(i);//设置生成pdf页数
				//4.生成PDF
				new PdfUtil().generatePeiHuo(document, dto.itemArr.get(i));
			}
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return MyConfig.getUrlDomain() + "/upload/pdf/" + fileName + ".pdf";
	}


	// 生成配货清单PDF
	public void generatePeiHuo(Document document, PeihuoDto.ItemDto itemDto) throws Exception {

		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{50, 100, 50, 100}, 0, 400);

			table1.addCell(createCell("配货清单：", textfont10, Element.ALIGN_LEFT, 4, 0));

			table1.addCell(createCell("单号：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getCodeBid(), textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("物流服务：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getNameWLFS(), textfont8, Element.ALIGN_LEFT, 0));

			table1.addCell(createCell("公司编码：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getCodeCompany(), textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("公司简称：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getNameCompany(), textfont8, Element.ALIGN_LEFT, 0));

			table1.addCell(createCell("商品总重量：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getWeightPredict(), textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("均分装箱：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getAverageBoxUpNum(), textfont8, Element.ALIGN_LEFT, 0));

			table1.addCell(createCell("货件创建方式：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getCreationMethod(), textfont8, Element.ALIGN_LEFT, 0));

			table1.addCell(createCell("欧代标：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getIsOd(), textfont8, Element.ALIGN_LEFT, 0));


			table1.addCell(createCell("备注：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getRemark(), textfont8, Element.ALIGN_LEFT, 0));

			table1.addCell(createCell("", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("", textfont8, Element.ALIGN_LEFT, 0));
		}

		// 空白表格
		PdfPTable tableBlank = createBlankTable(5.0f);

		// 表格2
		PdfPTable table2;
		{
			table2 = createTable(new float[]{10, 30, 40, 50, 40, 20, 20, 15, 20, 40, 40}, 1, 400);

			table2.addCell(createCell("明细", textfont10, Element.ALIGN_LEFT, 11, 0));

			table2.addCell(createCell("序号", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("图片", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("商品编码", textfont8, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("商品名称", textfont8, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("FNSKU", textfont8, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("计划发货数量", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("均分装箱数量", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("剩余库存", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("货位", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("透明标记编码", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("装箱情况", textfont8, Element.ALIGN_CENTER, 1));

			int i = 1;
			for(PeihuoDto.ItemDto.RowDto row : itemDto.getRows()) {
				table2.addCell(createCell("" + i, textfont6, Element.ALIGN_CENTER, 1));

				Image image = null;
				try {
					image = Image.getInstance(row.getImgGoods());
				} catch (Exception e) {
					image = Image.getInstance("https://file.mini-ease.com/upload/nomodule/202503/04/171232068.jpg");
				}
				image.setAlignment(Image.ALIGN_CENTER);
				image.scalePercent(20); //依照比例缩放
				image.setSpacingBefore(0f);
				table2.addCell(image);

				table2.addCell(createCell(row.getCodeGoods(), textfont6, Element.ALIGN_LEFT, 1));
				table2.addCell(createCell(row.getNameGoods(), textfont6, Element.ALIGN_LEFT, 1));
				table2.addCell(createCell(row.getFnsku(), textfont6, Element.ALIGN_LEFT, 1));
				table2.addCell(createCell(row.getNumPlanDeliver(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell(row.getNumAverageDeliver(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell(row.getNumStockLeft(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell(row.getLocationGoods(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell(row.getSignName(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell("", textfont6, Element.ALIGN_CENTER, 1)); // 装箱情况

				i++;
			}

		}
		document.add(table1);
		document.add(tableBlank);
		document.add(table2);
	}

	@Data
	@AllArgsConstructor
	public static class PeihuoDto{
		private List<ItemDto> itemArr; // 配货清单数组
		@Data
		@AllArgsConstructor
		public static class ItemDto{
			private String codeBid; // 头程计划单号
			private String codeCompany; // 公司编码
			private String weightPredict; // 商品总重量
			private String nameCompany; // 公司简称
			private String nameWLFS; // 物流服务名称
			private String averageBoxUpNum; // 均分装箱数量
			private String remark; // 备注
			private String creationMethod; // 货件创建方式
			private String isOd; // 是否打印欧代标
			private List<RowDto> rows; // 明细

			@Data
			@AllArgsConstructor
			public static class RowDto {
				private String codeGoods; // 商品编码
				private String nameGoods; // 商品名称
				private String fnsku; // FNSKU
				private String numPlanDeliver; // 计划发货数量
				private String numAverageDeliver; // 均分装箱数量
				private String locationGoods; // 货位
				private String locationStock; // 库区
				private String locationName; // 仓库名
				private String imgGoods; // 商品图片
				private String numStockLeft; // 剩余库存
				private String signName; // 透明标记（是/否）
			}
		}
	}


	@Data
	@AllArgsConstructor
	public static class GoodsDto{
		private String nameEn; // 商品英文名
		private String nameCn; // 商品中文名
		private String imgBarcode; // 条形码图片
		private Integer num; // 要打印的数量
	}


	/**
	 * FBA装箱单 - 配货清单-打印
	 */
	public static String europeanStandardPrint(String fileName, List<EuropeanStandardPrintDto> dtoList) {

		Rectangle rec = new RectangleReadOnly(500, 500);

		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			String dir = MyConfig.getProfile();
			String pdfFileName = "/pdf/" + fileName + ".pdf";
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();

			for (int i = 0; i < dtoList.size(); i++) {
				//4.生成PDF
				new PdfUtil().generateEuropeanStandard(document, dtoList.get(i));
			}
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return MyConfig.getUrlDomain() + "/upload/pdf/" + fileName + ".pdf";
	}

	// 生成欧代标PDF
	public void generateEuropeanStandard(Document document, EuropeanStandardPrintDto dto) throws Exception {
		document.newPage();
		// 表格
		PdfPTable table2;
		{
			table2 = createTable(new float[]{200, 280}, 1, 480);

			table2.addCell(createCellHeight("Description of Product:", textfont16Bold, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight(dto.getDescriptionProduct(), textfont16, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight("Model：", textfont16Bold, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight(dto.getModel(), textfont16, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight("EU Agent：", textfont16Bold, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight(dto.getEuAgent(), textfont16, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight("EU agent address：", textfont16Bold, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight(dto.getEuAgentAddress(), textfont16, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight("Manufacturer ：", textfont16Bold, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight(dto.getManufacturer(), textfont16, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight("Manufacturer Address ：", textfont16Bold, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight(dto.getManufactureraddress(), textfont16, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight("Age Range:", textfont16Bold, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight(dto.getAgeRange(), textfont16, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight("Production Date:", textfont16Bold, Element.ALIGN_CENTER, 1,30f));
			table2.addCell(createCellHeight(dto.getProductionDate(), textfont16, Element.ALIGN_CENTER, 1,30f));
		}

		// 空白表格
		PdfPTable tableBlank = createBlankTable(15.0f);

		// 表格
		PdfPTable table1;
		table1 = createTable(new float[]{240,240}, 0, 480);
		table1.addCell(createCell("MADE IN CHINA", textfont16Bold, Element.ALIGN_CENTER, 0));
		table1.addCell(createPdfCell("https://file.mini-ease.com/upload/nomodule/202410/09/211333666.png"));

		List<PdfPTable> pdfPTableList = new ArrayList<>();
		int size = dto.getTbFileInfos().size();
		double num = Math.ceil(size/5.0);
		int k = 0;
		float[] dynamicArray = new float[5];
		dynamicArray[0] = 96;
		dynamicArray[1] = 96;
		dynamicArray[2] = 96;
		dynamicArray[3] = 96;
		dynamicArray[4] = 96;
		for (int i = 0; i < num; i++) {
			PdfPTable table3 = createTable(dynamicArray, 0, 480);
			for (int j = 0; j < 5; j++) {
				if (dto.getTbFileInfos().size()-1 >= k ){
					table3.addCell(createPdfCell(dto.getTbFileInfos().get(k).getFilePath()));
				}else {
					table3.addCell(createBlankCell());
				}
				k = k + 1;
			}
			pdfPTableList.add(table3);
		}


		for (int i = 0; i < dto.getNumPlanDeliver(); i++) {
			document.newPage();
			document.add(table2);
			document.add(tableBlank);
			document.add(table1);
			for (PdfPTable pdfPTable : pdfPTableList){
				document.add(pdfPTable);
			}
		}
	}



	@Data
	@AllArgsConstructor
	public static class EuropeanStandardPrintDto{
		private String descriptionProduct; // 采购入库单号
		private String model; // 采购订单号
		private String euAgent; // 公司编码
		private String euAgentAddress; // 公司简称
		private String manufacturer; // 组装方案
		private String manufactureraddress; // 组装方式
		private String ageRange; // 商品信息
		private String productionDate; // 商品图片
		private Integer numPlanDeliver; // 数量
		private List<TbFileInfo> tbFileInfos; // 附件数量

	}
}
